<template>
  <div class="admin-login-container">
    <div class="login-card">
      <div class="header">
        <h2>管理员登录</h2>
        <div class="subtitle">外卖平台管理系统</div>
      </div>
      
      <el-form :model="loginForm" :rules="rules" ref="loginForm" @submit.native.prevent>
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            prefix-icon="el-icon-user"
            placeholder="请输入管理员账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            prefix-icon="el-icon-lock" 
            type="password" 
            placeholder="请输入密码">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading" 
            @click="handleLogin" 
            class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="links">
        <router-link to="/login">用户登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminLogin',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          
          // 模拟API请求
          setTimeout(() => {
            // 这里应该是实际的API调用验证管理员身份
            // 以下仅作演示
            if (this.loginForm.username === 'admin' && this.loginForm.password === 'admin123') {
              // 保存管理员token和信息
              localStorage.setItem('token', 'admin-token-example');
              localStorage.setItem('userInfo', JSON.stringify({
                id: 9999,
                username: this.loginForm.username,
                role: 'admin',
                name: '系统管理员'
              }));
              
              this.$message.success('登录成功');
              
              // 登录成功后跳转到管理员首页
              const redirect = this.$route.query.redirect || '/admin';
              this.$router.push(redirect);
            } else {
              this.$message.error('账号或密码错误');
            }
            this.loading = false;
          }, 1000);
        }
      });
    }
  }
}
</script>

<style scoped>
.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f6f8fa;
  background-image: linear-gradient(120deg, #e0f7fa 0%, #f5f7fa 100%);
}

.login-card {
  width: 380px;
  padding: 30px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h2 {
  color: #409EFF;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 14px;
  color: #909399;
}

.login-button {
  width: 100%;
}

.links {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}

.links a {
  color: #409EFF;
  text-decoration: none;
}

.links a:hover {
  text-decoration: underline;
}
</style> 